package com.carple.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.jws.WebService;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.carple.dao.BoardDao;
import com.carple.dao.RouteDao;
import com.carple.dto.Board;
import com.carple.dto.Member;
import com.carple.dto.Route;

@WebServlet("/route/showroute.action")
public class ShowRouteServlet extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		if (req.getSession().getAttribute("loginuser") == null) {
			resp.sendRedirect(
				"/carple/account/loginform.action?" + 
				"returnuri=" + req.getRequestURI());
			return;
		}
		
		
		
//		String boardNo = req.getParameter("boardno");
//		if(boardNo==null || boardNo.length() == 0){
//			resp.sendRedirect("carple/board/list.action");
//			return;
//		}

		RouteDao dao = new RouteDao();
		ArrayList<Route>routesOfWriter = dao.getRouteByEmailId(req.getParameter("emailid"));
		ArrayList<Route>routesOfReader = dao.getRouteByEmailId(((Member)(req.getSession().getAttribute("loginuser"))).getEmailId());
		
		if(routesOfWriter == null){
			resp.sendRedirect("/carple/board/list.action");
			return;
		}
		
		//조회된 데이터를 jsp에서 처리할수 있도록 request객체에 저장
		req.setAttribute("routesOfWriter", routesOfWriter);
		req.setAttribute("routesOfReader", routesOfReader);
		
				
		
		RequestDispatcher dispatcher = req.getRequestDispatcher("/WEB-INF/views/route/showroute.jsp");
		dispatcher.forward(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		doPost(req, resp);
	}

}
